<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" 
import="orm.*" import="java.util.*"errorPage="errorPage.jsp" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% 	User tea  =(User) session.getAttribute("Teacher");  
	if (tea == null){
		response.sendRedirect(path + "/index.jsp");
	}
%>
<% 	String courseOpt = ""; 
	String templatesOpt = "";
	String testsOpt = "";
	List courses = (List)session.getAttribute("courseList");
	Map templateMap = (Map)session.getAttribute("templateMap");
	Map testMap = (Map)session.getAttribute("testMap");
	
	Map tests = null;
	
	if (courses == null || courses.size() == 0){
		courseOpt = "<option value=''>您没有课程！</option>";
	}
	else {
		for(int i = 0; i < courses.size(); i++){
			Course course = (Course)courses.get(i);
			String name = course.getCoursename();
			if (i == 0){
				courseOpt += "<option value = '" + name + "' selected='selected' >" + name + "</option>";
				tests = (Map)testMap.get(name);
				if (tests != null && tests.size() > 0){
					Set tIds = tests.keySet();
					Iterator it = tIds.iterator();
					for (int j = 1; it.hasNext();){
						Long o = (Long)it.next();
						Test t = (Test)tests.get(o);
						if (j == 1){
							testsOpt += "<option selected = 'selected' value = '" + o + "'>" + t.getTestname() + "</option>";
							Set templates = (Set)templateMap.get(o);
							if (templates != null && templates.size() > 0){
								int tcnt = 1;
								for (Iterator it2 = templates.iterator(); it2.hasNext();){
									Testtemplate temp = (Testtemplate)it2.next();
									Long tempid = new Long(temp.getTempid());
									if (tcnt == 1){
										templatesOpt += "<option selected = 'selected' value = '" + tempid + "'>" + temp.getTempName() + "</option>";
										tcnt++;
										continue;
									}
									templatesOpt += "<option value = '" + tempid + "'>" + temp.getTempName() + "</option>";
								}
								session.setAttribute("templates", templates);
							}
							else{
								templatesOpt = "<option value=''>该考试下没有模板！</option>";
								session.removeAttribute("templates");
							}
							j++;
							continue;
						}
						testsOpt += "<option value = '" + o + "'>" + t.getTestname() + "</option>";
					}
				}
				else{
					testsOpt = "<option value=''>该课程下没有考试！</option>";
				}
				continue;
			}
			courseOpt += "<option value = '" + name + "'>" + name + "</option>";
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href = "<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/tStyles.css" rel="stylesheet" type="text/css" />
<title>试卷模板管理</title>
<script language="javascript" src="js/AjaxRequest.js">
</script>
<script type="text/javascript">
	<%if (request.getAttribute("success") != null){%>alert("<%=request.getAttribute("success")%>");<%request.removeAttribute("success");}
	else if (request.getAttribute("error") != null){%>alert("<%=request.getAttribute("error")%>");<%request.removeAttribute("error");}%>
	var flag = 2;
	function showTemplates(){
		var t = this.testN.value;
		var loader2=new net.AjaxRequest("ajax.do?action=getTemplates&t=" + t,show,onerror,"GET");
	}
	function display(f){
		var loader3=new net.AjaxRequest("ajax.do?action=templates_display&flag=" + f,displayButton,onerror,"GET");
	}
	function onerror(){
	}
	function showTests(){
		var c = this.course.value;
		var loader=new net.AjaxRequest("ajax.do?action=getTests&c=" + c,showT,onerror,"GET");
	}
	function show(){
		document.getElementById("st_2").innerHTML=this.req.responseText;
		display(flag);
	}
	function showT(){
		document.getElementById("testN").innerHTML=this.req.responseText;
		flag = 1;
		showTemplates();
	}
	function displayButton(){
		if (flag == 1){
			flag = 2;
		}
		var str = this.req.responseText;
		if (str.indexOf("displayAll")!= -1){
			document.getElementById("addT").style.visibility = 'visible';
			document.getElementById("modifyT").style.visibility = 'visible';
			document.getElementById("deleteT").style.visibility = 'visible';
		}
		else if (str.indexOf("null") != -1){
			document.getElementById("addT").style.visibility = 'hidden';
			document.getElementById("modifyT").style.visibility = 'hidden';
			document.getElementById("deleteT").style.visibility = 'hidden';
		}
		else{
			document.getElementById("addT").style.visibility = 'visible';
			document.getElementById("modifyT").style.visibility = 'hidden';
			document.getElementById("deleteT").style.visibility = 'hidden';
		}
	}
	function operateTemplates(str){
		if (str.indexOf("addT") != -1){
			window.location.href = "manageTemplates.do?action=templates&method=add&course=" + this.course.value + "&testId=" + this.testN.value;
		}
		else if (str.indexOf("modifyT") != -1){
			window.location.href = "manageTemplates.do?action=templates&method=view&tid=" + this.st_2.value 
				+ "&course=" + this.course.value + "&testId=" + this.testN.value;
		}
		else if (str.indexOf("deleteT") != -1){
			var opt = confirm("本次删除不可恢复，是否删除该模板?");
			if (opt == true){
				window.location.href = "manageTemplates.do?action=deleteTemplate&tid=" + this.st_2.value + "&course=" + this.course.value;
			}
		}
	}
</script>
</head>
<body>
	<!-- CONTAINER START -->
<div id = "container">
<!-- HEADER START -->
        <div id = "top">
        	<div id = "navigation">
            	<html:link page="/teacher/teacher.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;首页</html:link>
                <html:link page="/teacher/notice.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;通知</html:link>
                <html:link action="manageQuestions.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;题库</html:link>
                <html:link action="manageKnowledges.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;知识点</html:link>
                <html:link action="manageTemplates.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;试卷模板</html:link>
                <html:link action="gradeManagement.do?action=startMana" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;成绩管理</html:link>
                <html:link page="/teacher/studentMana.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;学生管理</html:link>
            </div>
        </div>
        <!-- HEADER END -->
        
	<!-- LOGIN START -->
        <div id = "login">
        	<div id = "name">
        	<%=tea.getRealname()%>
            </div>
        	<html:link page="/teacher/teacherInfo.jsp" styleClass="viewInfo">·查询个人信息</html:link>
          	<p><html:link page="/teacher/teaModifyPwd.jsp" styleClass="modifyPwd">·修改密码</html:link></p>
            <p><html:link page="/teacher/letter.jsp" styleClass="letter">·站内信</html:link></p>
            <p><html:link forward="logout" styleClass="logout">·注销登录</html:link></p>
  </div>
        <!-- LOGIN END -->
        
        <!-- CLOCK START -->
  <div id = "clock">
        	<iframe src="index/clock.html" height="407" width="296" frameborder="0" scrolling="no"
            		marginheight="0" marginwidth="0" id="clockFrame" name="clockFrame"></iframe>
</div>
        <!-- CLOCK END -->
        
        <!-- MAIN START -->
      	<div id = "main">
        	<center>
        	<div id = "subject">
            	<br /><br />
            	<span class="text"><strong>科目:</strong></span>
              		<select name="course" id="course" style="width:100px;" 
              		onchange="javascript:showTests();">
                		<%=courseOpt %>            
             		</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
             	<span class="text"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;考试:</strong></span>
                    <select name="testN" id="testN" style="width:100px;"
                        onchange="javascript:showTemplates();">
                              <%=testsOpt %>
                    </select>
                    <input type="button" value="发布新考试" onclick="window.location.href='teacher/testSetting.jsp'"/><br/>
            </div>
            <div id = "templates">
       	  		<table border="1">
                	<tr>
    					<td width="332" height="28" class="st_text"><strong>已有模板</strong></td>
  					</tr>
  					<tr>
    					<td height="278" valign="top">
                          <select name="st_2" size="20" id="st_2" style="width:330px; ">
                          <%=templatesOpt %>
                          </select></td>
  					</tr><% if (courses != null && courses.size() != 0){ %>
  					<tr>
    					<td height="41" align="center">
                        	<input type="button" name="addT" id="addT" value="添加模板" style="width:80px; height:40px;
                        	<%if (tests == null || tests.size() <= 0) {%>visibility:hidden<%} %> "
                        	 onclick="operateTemplates(this.name);"/>
      						&nbsp;
        					<input type="button" name="modifyT" id="modifyT" value="查看模板" style="width:80px; height:40px; 
        					<%if (session.getAttribute("templates") == null) {%>visibility:hidden<%} %>"
        					 onclick="operateTemplates(this.name);" />
                            &nbsp;
                            <input type="button" name="deleteT" id="deleteT" value="删除模板" style="width:80px; height:40px; 
                            <%if (session.getAttribute("templates") == null) {%>visibility:hidden<%} %>"
                             onclick="operateTemplates(this.name);" /></td>
                    </tr><%} %>
                </table>
            </div>
            </center>
   	  	</div>
	<!-- MAIN END -->
        
	<!-- FOOTER START -->
	<div id = "footer">
  </div>
	<!-- FOOTER END -->
        
</div>
    <!-- CONTAINER END -->
</body>
</html>